package com.h3c.ptability.support;

import cn.hutool.core.util.StrUtil;
import lombok.Data;

/**
 * @author chenyifan
 * @date 2022/5/17 14:28
 */
@Data
public class ResultData<T> {

    private int status;
    private String message;
    private T data;
    private long timestamp;


    public ResultData() {
        this.timestamp = System.currentTimeMillis();
    }


    public static <T> ResultData<T> success(String message) {
        ResultData<T> resultData = new ResultData<>();
        resultData.setStatus(ReturnCodeEnum.RC200.getCode());
        resultData.setMessage(message);
        return resultData;
    }

    public static <T> ResultData<T> success(String message,String... params) {
        ResultData<T> resultData = new ResultData<>();
        resultData.setStatus(ReturnCodeEnum.RC200.getCode());
        resultData.setMessage(StrUtil.format(message, params));
        return resultData;
    }

    public static <T> ResultData<T> data(T data) {
        ResultData<T> resultData = new ResultData<>();
        resultData.setStatus(ReturnCodeEnum.RC200.getCode());
        resultData.setMessage(ReturnCodeEnum.RC200.getMessage());
        resultData.setData(data);
        return resultData;
    }

    public static <T> ResultData<T> data(T data, String message, String... params) {
        ResultData<T> resultData = new ResultData<>();
        resultData.setStatus(ReturnCodeEnum.RC200.getCode());
        resultData.setMessage(StrUtil.format(message, params));
        resultData.setData(data);
        return resultData;
    }

    public static <T> ResultData<T> fail(int code, String message, String... params) {
        ResultData<T> resultData = new ResultData<>();
        resultData.setStatus(code);
        resultData.setMessage(StrUtil.format(message, params));
        return resultData;
    }

    public static <T> ResultData<T> fail(String message, String... params) {
        ResultData<T> resultData = new ResultData<>();
        resultData.setStatus(ReturnCodeEnum.RC400.getCode());
        resultData.setMessage(StrUtil.format(message, params));
        return resultData;
    }

    public static <T> ResultData<T> fail() {
        ResultData<T> resultData = new ResultData<>();
        resultData.setStatus(ReturnCodeEnum.RC400.getCode());
        resultData.setMessage(ReturnCodeEnum.RC400.getMessage());
        return resultData;
    }
}
